﻿Imports System.Drawing

Partial Class pages_products_ProductImage : Inherits CPage

#Region "Querystring"
    Public ReadOnly Property ProductId() As Integer
        Get
            Return CWeb.RequestInt("productId")
        End Get
    End Property
#End Region

#Region "Data"
    Public ReadOnly Property [Product]() As CProduct
        Get
            Product = CProduct.Cache.GetById(ProductId)
            If IsNothing(Product) Then CSitemap.RecordNotFound("Product", ProductId)
        End Get
    End Property
#End Region

#Region "Navigation"
    Private Sub Refresh()
        Response.Redirect(CSitemap.ProductImage(Me.Product.ProductId))
    End Sub
    Private Sub ReturnToList()
        Response.Redirect(CSitemap.Products(Nothing, Product.ProductTypeId, Integer.MinValue, Integer.MinValue, Integer.MinValue, Integer.MinValue, Integer.MinValue, Nothing))
    End Sub
#End Region

#Region "Event Handlers - Page"
    Protected Overrides Sub PageInit()
        ddIllustrator.DataSource = CScribe.Cache.ForImages
        ddIllustrator.DataBind()
        ddIllustrator.BlankItem("-- Credited To --", 0)

        With Product

            MenuSelected = "Products"
            UnbindSideMenu()
            AddMenuSide(.ProductType.Plural & "...", CSitemap.Products(Nothing, Product.ProductTypeId, Integer.MinValue, Integer.MinValue, Integer.MinValue, Integer.MinValue, Integer.MinValue, Nothing))
            AddMenuSide(Product.TypeName & " Details", CSitemap.ProductEdit(ProductId))
            AddMenuSide("Image" & IIf(.HasImage, "*", String.Empty), CSitemap.ProductImage(ProductId), True)
            If .IsSet Then AddMenuSide(CUtilities.NameAndCount("Contents", .Children), CSitemap.ProductContents(ProductId))
            If .CanHaveTranscript Then
                AddMenuSide("Transcription" & IIf(.HasTranscript, "*", String.Empty), CSitemap.ProductTranscription(ProductId))
            End If
            AddMenuSide("Vimeo Link" & IIf(.HasVideosOnVimeo, "*", String.Empty) & IIf(.HasVideosOnYouTube, "*", String.Empty), CSitemap.ProductVimeo(ProductId))
            If .CanHaveFiles Then
                AddMenuSide("Dropbox Files", CSitemap.ProductFiles(ProductId))
            Else
                AddMenuSide(CUtilities.NameAndCount("Epub/Mobi eBooks", CFile.Cache.GetByProductId(Me.ProductId)), CSitemap.ProductFiles(ProductId))
            End If
            If Not .IsFree OrElse .PaymentsCount > 0 OrElse .OrdersCountCached > 0 Then
                AddMenuSide(CUtilities.NameAndCount("Orders", .OrdersCountCached), CSitemap.OrdersForProduct(ProductId))
                AddMenuSide(CUtilities.NameAndCount("Payments", .PaymentsCount), CSitemap.PaymentsForProduct(ProductId))
            End If
            If .HasParent Then AddMenuSide("Back to Set...", CSitemap.ProductContents(.ProductBelongsToProductId))


            imgProductImage.FolderPath = CSitemap.ProductUploads(True)
            imgProductImage.FileName = .ProductImage

            lnkThumb.Visible = Not String.IsNullOrEmpty(.ProductThumb)
            lnkThumb.NavigateUrl = CSitemap.ProductThumb(Product)
            lnkThumb.Text = String.Concat(.ProductThumb, " (", CUtilities.FileSize(Server.MapPath(lnkThumb.NavigateUrl)), ")").Replace("()", "")
            lnkThumb.Target = "_blank"

            lnkTiny.Visible = Not String.IsNullOrEmpty(.ProductTiny)
            lnkTiny.NavigateUrl = CSitemap.ProductTiny(Product)
            lnkTiny.Text = String.Concat(.ProductTiny, " (", CUtilities.FileSize(Server.MapPath(lnkTiny.NavigateUrl)), ")").Replace("()", "")
            lnkTiny.Target = "_blank"


            ddIllustrator.ValueInt = .ProductImageScribeId

            'Default image
            imgProductImage.FolderPath = CSitemap.ProductUploads(True)
            If .HasParent Then
                imgDefault.FolderPath = CSitemap.ProductUploads(True)
                imgDefault.FileName = .BelongsTo.ProductImage
            ElseIf Not String.IsNullOrEmpty(.Speaker.SpeakerImage) Then
                imgDefault.FolderPath = CSitemap.SpeakerUploads
                imgDefault.Value = .Speaker.SpeakerImage
            Else
                imgDefault.FolderPath = CSitemap.SettingUploads
                imgDefault.Value = CSetting.SermonImage
            End If
        End With
    End Sub
#End Region

    Protected Sub SaveImage()

    End Sub


    Protected Sub imgProductImage_DeleteClick() Handles imgProductImage.DeleteClick
        Dim imagePath As String = CSitemap.ProductImage(Product, False)
        Dim thumbPath As String = CSitemap.ProductThumb(Product, False)
        Dim tinyPath As String = CSitemap.ProductTiny(Product, False)
        If IO.File.Exists(imagePath) Then IO.File.Delete(imagePath)
        If IO.File.Exists(thumbPath) Then IO.File.Delete(thumbPath)
        If IO.File.Exists(tinyPath) Then IO.File.Delete(tinyPath)
        With Product
            .ProductImage = Nothing
            .ProductThumb = Nothing
            .ProductTiny = Nothing
            .Save()
        End With
        Response.Redirect(Request.RawUrl)
    End Sub

    Protected Sub btnSave_Click(sender As Object, e As System.EventArgs) Handles btnSave.Click
        With Product
            If imgProductImage.HasFile Then
                imgProductImage.FolderPath = CSitemap.ProductUploads(False)
                .ProductImage = imgProductImage.SaveFile()
                .ProductThumb = .ProductImage
            End If
            .ProductImageScribeId = ddIllustrator.ValueInt
            .Save()


            If imgProductImage.HasFile Then
                Dim imgPath As String = CSitemap.ProductImage(Product, False)

                Dim thumbName As String = IO.Path.GetFileNameWithoutExtension(.ProductImage) & "_thumb" & IO.Path.GetExtension(imgPath)
                Dim tinyName As String = IO.Path.GetFileNameWithoutExtension(.ProductImage) & "_tiny" & IO.Path.GetExtension(imgPath)

                Dim thumbPath As String = CSitemap.ProductUpload(thumbName, False)
                Dim tinyPath As String = CSitemap.ProductUpload(tinyName, False)

                Dim img As Image
                Try
                    img = Image.FromFile(imgPath)
                Catch ex As Exception
                    img = Image.FromFile(Server.MapPath("~/images/error.gif"))
                End Try

                SaveAsWidth(150, thumbPath, img)
                SaveAsWidth(75, tinyPath, img)
                img.Dispose()

                .ProductThumb = thumbName
                .ProductTiny = tinyName


                .Save()
            End If

        End With
        Refresh()
    End Sub

    Protected Sub btnCancel_Click(sender As Object, e As System.EventArgs) Handles btnCancel.Click
        Refresh()
    End Sub




    Private Sub SaveAsWidth(width As Short, path As String, img As Image)
        Dim height As Short = GetHeight(width, img)
        Dim thumb As Image = img.GetThumbnailImage(width, height, Nothing, IntPtr.Zero)
        thumb.Save(path)
        thumb.Dispose()
    End Sub
    Private Function GetHeight(width As Short, img As Image) As Short
        Return CInt(width * CDbl(img.Height) / CDbl(img.Width))
    End Function
End Class
